/*
 * This file is part of the OregonCore Project. See AUTHORS file for Copyright information
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program. If not, see <http://www.gnu.org/licenses/>.
 */

 /* ScriptData
 SDName: Bloodmyst_Isle
 SD%Complete: 80
 SDComment: Quest support: 9711, 9670, 9756(gossip items text needed).
 SDCategory: Bloodmyst Isle
 EndScriptData */

 /* ContentData
 mob_webbed_creature
 npc_captured_sunhawk_agent
 EndContentData */

#include "ScriptMgr.h"
#include "ScriptedCreature.h"
#include "ScriptedGossip.h"

 /*######
 ## mob_matis_the_cruel
 ######*/
enum misc
{
    QUEST_MATIS_THE_CRUEL = 9711,
    FACTION_ORGINAL = 1701,
    FACTION_FRIENDLY = 35,
    NPC_TRACKER = 17853,
};

class mob_matis_the_cruel : public CreatureScript
{
public:
    mob_matis_the_cruel() : CreatureScript("mob_matis_the_cruel") { }
    struct mob_matis_the_cruelAI : public ScriptedAI
    {
        mob_matis_the_cruelAI(Creature* c) : ScriptedAI(c) {}

        void Reset()
        {
            me->SetFaction(FACTION_ORGINAL);
        }

        void EnterCombat(Unit* who)
        {
            if (Creature* tracker = GetClosestCreatureWithEntry(me, NPC_TRACKER, 35.0f))
            {
                tracker->Say("We've got you now, Mathis the Cruel!", LANG_UNIVERSAL, me->GetGUID());
                me->Say("You will regret this, maggot.", LANG_UNIVERSAL, me->GetGUID());
            }
        }

        void creditPlayer()
        {
            Map* map = me->GetMap();
            Map::PlayerList const &PlayerList = map->GetPlayers();

            for (auto itr = PlayerList.begin(); itr != PlayerList.end(); ++itr)
            {
                if (Player* player = itr->GetSource())
                    if (me->IsWithinDistInMap(player, 20.0f) && (player->GetQuestStatus(QUEST_MATIS_THE_CRUEL) == QUEST_STATUS_INCOMPLETE))
                        player->GroupEventHappens(QUEST_MATIS_THE_CRUEL, me);
            }
        }

        void UpdateAI(const uint32 diff)
        {
            if (!UpdateVictim())
                return;

            if (me->GetHealthPct() <= 25)
            {
                me->CombatStop();
                me->SetFaction(FACTION_FRIENDLY);
                me->SetStandState(UNIT_STAND_STATE_KNEEL);
                if (Creature* tracker = GetClosestCreatureWithEntry(me, NPC_TRACKER, 35.0f))
                {
                    tracker->Say("Return to Kuros. I will bring him to Blood Watch", LANG_UNIVERSAL, me->GetGUID());
                    creditPlayer();
                    tracker->DespawnOrUnsummon(10000);
                }
                me->DespawnOrUnsummon(10000);
            }

            DoMeleeAttackIfReady();
        }
    };

    CreatureAI* GetAI(Creature* pCreature) const
    {
        return new mob_matis_the_cruelAI(pCreature);
    }

};

/*######
## mob_webbed_creature
######*/

//possible creatures to be spawned
const uint32 possibleSpawns[32] = { 17322, 17661, 17496, 17522, 17340, 17352, 17333, 17524, 17654, 17348, 17339, 17345, 17359, 17353, 17336, 17550, 17330, 17701, 17321, 17680, 17325, 17320, 17683, 17342, 17715, 17334, 17341, 17338, 17337, 17346, 17344, 17327 };

class mob_webbed_creature : public CreatureScript
{
public:
    mob_webbed_creature() : CreatureScript("mob_webbed_creature") { }

    struct mob_webbed_creatureAI : public ScriptedAI
    {
        mob_webbed_creatureAI(Creature* c) : ScriptedAI(c) {}

        void Reset()
        {
        }

        void EnterCombat(Unit* /*who*/)
        {
        }

        void JustDied(Unit* Killer)
        {
            uint32 spawnCreatureID = 0;

            switch (urand(0, 2))
            {
            case 0:
                spawnCreatureID = 17681;
                if (Killer->GetTypeId() == TYPEID_PLAYER)
                    CAST_PLR(Killer)->KilledMonsterCredit(spawnCreatureID, me->GetGUID());
                break;
            case 1:
            case 2:
                spawnCreatureID = possibleSpawns[urand(0, 30)];
                break;
            }

            if (spawnCreatureID)
                DoSpawnCreature(spawnCreatureID, 0, 0, 0, me->GetOrientation(), TEMPSUMMON_TIMED_DESPAWN_OUT_OF_COMBAT, 60000);
        }
    };

    CreatureAI* GetAI(Creature* pCreature) const
    {
        return new mob_webbed_creatureAI(pCreature);
    }

};

/*######
## npc_captured_sunhawk_agent
######*/

#define C_SUNHAWK_TRIGGER 17974

#define GOSSIP_HELLO_CSA     "[PH] "
#define GOSSIP_SELECT_CSA1   "[PH] "
#define GOSSIP_SELECT_CSA2   "[PH] "
#define GOSSIP_SELECT_CSA3   "[PH] "
#define GOSSIP_SELECT_CSA4   "[PH] "
#define GOSSIP_SELECT_CSA5   "[PH] "

class npc_captured_sunhawk_agent : public CreatureScript
{
public:
    npc_captured_sunhawk_agent() : CreatureScript("npc_captured_sunhawk_agent") { }

    bool OnGossipHello(Player* pPlayer, Creature* pCreature) override
    {
        if (pPlayer->HasAura(31609, 1) && pPlayer->GetQuestStatus(9756) == QUEST_STATUS_INCOMPLETE)
        {
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_HELLO_CSA, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 1);
            pPlayer->SEND_GOSSIP_MENU(9136, pCreature->GetGUID());
        }
        else
            pPlayer->SEND_GOSSIP_MENU(9134, pCreature->GetGUID());

        return true;
    }

    bool OnGossipSelect(Player* pPlayer, Creature* pCreature, uint32 /*uiSender*/, uint32 uiAction) override
    {
        switch (uiAction)
        {
        case GOSSIP_ACTION_INFO_DEF + 1:
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_SELECT_CSA1, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 2);
            pPlayer->SEND_GOSSIP_MENU(9137, pCreature->GetGUID());
            break;
        case GOSSIP_ACTION_INFO_DEF + 2:
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_SELECT_CSA2, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 3);
            pPlayer->SEND_GOSSIP_MENU(9138, pCreature->GetGUID());
            break;
        case GOSSIP_ACTION_INFO_DEF + 3:
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_SELECT_CSA3, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 4);
            pPlayer->SEND_GOSSIP_MENU(9139, pCreature->GetGUID());
            break;
        case GOSSIP_ACTION_INFO_DEF + 4:
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_SELECT_CSA4, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 5);
            pPlayer->SEND_GOSSIP_MENU(9140, pCreature->GetGUID());
            break;
        case GOSSIP_ACTION_INFO_DEF + 5:
            pPlayer->ADD_GOSSIP_ITEM(GOSSIP_ICON_CHAT, GOSSIP_SELECT_CSA5, GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF + 6);
            pPlayer->SEND_GOSSIP_MENU(9141, pCreature->GetGUID());
            break;
        case GOSSIP_ACTION_INFO_DEF + 6:
            pPlayer->CLOSE_GOSSIP_MENU();
            pPlayer->TalkedToCreature(C_SUNHAWK_TRIGGER, pCreature->GetGUID());
            break;
        }
        return true;
    }
};

#define SPELL_SYMBOL_OF_LIFE  8593

class npc_young_furblog_shaman : public CreatureScript
{
public:
    npc_young_furblog_shaman() : CreatureScript("npc_young_furblog_shaman") {}

    struct npc_young_furbolg_shamanAI : public ScriptedAI
    {
        npc_young_furbolg_shamanAI(Creature* c) : ScriptedAI(c) {}

        void Reset()
        {
            reset_timer = 900000;
            me->SetStandState(UNIT_STAND_STATE_DEAD);
            me->SetUInt32Value(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_DEAD);
        }

        uint32 reset_timer;

        void SpellHit(Unit* /*pWho*/, const SpellEntry* pSpell)
        {
            //When hit with ressurection say text
            if (pSpell->Id == SPELL_SYMBOL_OF_LIFE)
            {
                me->SetStandState(UNIT_STAND_STATE_STAND);
                me->SetUInt32Value(UNIT_DYNAMIC_FLAGS, 0);
            }
        }

        void UpdateAI(const uint32 uiDiff)
        {
            if (!UpdateVictim())
            {
                if (reset_timer <= uiDiff)
                {
                    me->SetStandState(UNIT_STAND_STATE_DEAD);
                    me->SetUInt32Value(UNIT_DYNAMIC_FLAGS, UNIT_DYNFLAG_DEAD);
                    reset_timer = 900000;
                }
                else reset_timer -= uiDiff;
            }
        }
    };

    CreatureAI* GetAI(Creature* pCreature) const
    {
        return new npc_young_furbolg_shamanAI(pCreature);
    };
};


void AddSC_bloodmyst_isle()
{
    new mob_webbed_creature();
    new npc_captured_sunhawk_agent();
    new mob_matis_the_cruel();
    new npc_young_furblog_shaman();

}

